﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tellstick;
using System.IO;
using Alfred.Ressource;

namespace Alfred
{
    class Scenario
    {
        public string name { get; set; }
        public Dictionary<string, int> pieces { get; set; }
        public Tuple<string, int> music { get; set; }

        public void launchScenario()
        {
            Playlist pl = new Playlist();

            foreach (var pair in pieces)
            {
                Device.Light(pair.Value, pair.Key);
            }

            if (music.Item2 == 1)
                pl.createPlaylistByArtist(music.Item1);
            else
                pl.createPlaylistByGenre(music.Item1);
            pl.launchPlaylist(music.Item1);
        }

        public static Scenario getScenarioByName(string name)
        {
            return getScenarios().Where(s => s.name == name).First();
        }

        public static List<Scenario> getScenarios()
        {
            StreamReader r = new StreamReader(Paths.path_scenario, Encoding.UTF8);
            List<Scenario> scenarios = new List<Scenario>();

            string line="";
            while ((line = r.ReadLine()) != null)
            {
                string[] infos = line.Split('\t');
                Dictionary<string, int> pieces = new Dictionary<string, int>();

                string[] musicInfos = infos[1].Split('=');

                for (int i = 2; i < infos.Length; i++)
                {
                    string[] pieceInfos = infos[i].Split('=');
                    pieces.Add(pieceInfos[0], Int32.Parse(pieceInfos[1]));
                }
                scenarios.Add(new Scenario() { name = infos[0], music = new Tuple<string, int>(musicInfos[0], Int32.Parse(musicInfos[1])), pieces = pieces });
            }
            return scenarios;
        }

        public static List<string> getScenarioStringList()
        {
            return getScenarios().Select(s => s.name).ToList();
        }
    }
}
